#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
@File    :   admin.py
@Time    :   2025/10/24 20:42:23
@Author  :   Rico Chen 
@Mail    :   55059@qq.com
@Version :   1.0
@Desc    :   重写认证、注册等页面
'''


from datetime import date
from typing import Optional
from fastapi_amis_admin.utils.translation import i18n as _
from fastapi_amis_admin.models.fields import Field
from fastapi_user_auth.admin import AuthAdminSite, RoleAdmin, UserAuthApp
from fastapi_user_auth.admin import UserLoginFormAdmin, UserRegFormAdmin
from starlette.requests import Request
from fastapi_user_auth.auth.models import BaseRole, BaseUser
from fastapi_amis_admin.admin.admin import FormAdmin
from fastapi_amis_admin.amis.components import (
    Action,
    ActionType,
    ButtonToolbar,
    Form,
    Grid,
    Horizontal,
    Html,
    Page,
    PageSchema,
)

def attach_page_head(page: Page) -> Page:
    desc = _("铁塔基站管理系统，一个可以控制字和数据级权限的基站管理系统！欢迎使用^_^")
    page.body = [
        Html(
            html=f'<div style="display: flex; justify-content: center; align-items: center; margin: 96px 0px 8px;">'
            f'<img src="/static/images/logo.png" alt="logo" style="margin-right: 8px; '
            f'width: 48px;"><span style="font-size: 32px; font-weight: bold;">铁塔基站管理系统</span></div>'
            f'<div style="width: 100%; text-align: center; color: rgba(0, 0, 0, 0.45); margin-bottom: 40px;">{desc}</div>'
        ),
        Grid(columns=[{"body": [page.body], "lg": 2, "md": 4, "valign": "middle"}], align="center", valign="middle"),
    ]
    return page

class MyUserLoginFormAdmin(UserLoginFormAdmin):
    unique_id = "Auth>MyUserLoginFormAdmin"
    async def get_page(self, request: Request) -> Page:
        #这里查看UserLoginFormAdmin的源代码，仿写。不能直接调用上一级的get_page方法，因为会导致页面重叠
        page = await super(FormAdmin, self).get_page(request)
        page.body = await self.get_form(request)
        # 添加页面头部
        return attach_page_head(page)

class MyUserRegFormAdmin(UserRegFormAdmin):
    unique_id = "Auth>MyUserRegFormAdmin"
    # 重写get_page方法，实现自定义页面
    async def get_page(self, request: Request) -> Page:
        #这里查看UserRegFormAdmin的源代码，仿写。不能直接调用上一级的get_page方法，因为会导致页面重叠
        page = await super(FormAdmin, self).get_page(request)
        page.body = await self.get_form(request)
        # 添加页面头部
        return attach_page_head(page)